package edu.istu.asu07_1.live.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister.Pack;

import edu.istu.asu07_1.live.configs.Constants;
import edu.istu.asu07_1.live.model.Model;
import edu.istu.asu07_1.live.model.Taktable;
import edu.istu.asu07_1.live.model.agents.Agent;

public class Vizualizator implements Taktable{

	private DefaultFormBuilder builder;
	private CellConstraints cc;
	private JPanel content;

	private int width;
	private int height;

	private Model model;
	private List<Agent> agentsList;

	public Vizualizator(){
		this.width = Constants.FIELD_WIDTH;
		this.height = Constants.FIELD_HEIGHT;

		init();
	}

	public Vizualizator(Model model){
		this.width = Constants.FIELD_WIDTH;
		this.height = Constants.FIELD_HEIGHT;
		this.model = model;
		agentsList = model.getAgentsList();

		init();
	}

	public Vizualizator(Model model, int width, int height){
		this.width = width;
		this.height = height;
		this.model = model;
		agentsList = model.getAgentsList();

		init();
	}

	private void init(){
		content = new JPanel();
		content.setDoubleBuffered(true);
		content.setBackground(Color.WHITE);

		StringBuffer w = new StringBuffer("fill:p:grow,");
		StringBuffer h = new StringBuffer("fill:p:grow,");


		for(int i = 0; i<width; i++){
			w.append(CELL_SIZE+",");
		}
		for(int j = 0; j<height; j++){
			h.append(CELL_SIZE+",");
		}

		w.append("fill:p:grow");
		h.append("fill:p:grow");

		FormLayout backgroundLayout = new FormLayout(
				w.toString(),
				h.toString()
		);

		cc = new CellConstraints();
//		builder = new DefaultFormBuilder(backgroundLayout, new FormDebugPanel());
		builder = new DefaultFormBuilder(backgroundLayout);

		builder.setBackground(Constants.BACKGROUND);

		content.add(builder.getPanel());
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public Model getModel() {
		return model;
	}

	public void setModel(Model model) {
		this.model = model;
		this.agentsList = model.getAgentsList();
	}

	public JPanel getContent() {
		return content;
	}

	public void setContent(JPanel content) {
		this.content = content;
	}

	@Override
	public void takt() {
		Agent a;
		builder.getPanel().removeAll();
		for(int i=0; i<agentsList.size(); i++){
			a = agentsList.get(i);
//			В виду не ясных пока мне причин, коректное отображение достигается сдвигом агента
//			На ячейку вперед
			builder.add(a.getImageIcon(), cc.xy(a.getxCoord()+1, a.getyCoord()+1));
		}
		content.repaint();
		content.revalidate();
	}

	public static final int CELL_SIZE = 25;

}
